package com.suep.data_analysis.mapper;

import com.suep.data_analysis.dto.UserConfigDetailDto;
import com.suep.data_analysis.model.Agent;
import com.suep.data_analysis.model.AgentSkills;
import com.suep.data_analysis.model.Armament;
import com.suep.data_analysis.model.UserConfig;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper(uses = {
        AgentMapper.class,
        AgentSkillsMapper.class,
        UserConfigListMapper.class
})
public interface UserConfigDetailMapper {
    UserConfigDetailMapper INSTANCE = Mappers.getMapper(UserConfigDetailMapper.class);

    // 单个详情转换（修正Mapping配置）
    @Mapping(source = "userConfig.configId", target = "configId")
    @Mapping(source = "userConfig.userId", target = "userId")
    // 角色基础属性
    @Mapping(source = "userConfig.baseHp", target = "baseHp")
    @Mapping(source = "userConfig.baseAtk", target = "baseAtk")
    @Mapping(source = "userConfig.baseDef", target = "baseDef")
    @Mapping(source = "userConfig.baseImpact", target = "baseImpact")
    @Mapping(source = "userConfig.critRate", target = "critRate")
    @Mapping(source = "userConfig.critDmg", target = "critDmg")
    @Mapping(source = "userConfig.baseControl", target = "baseControl")
    @Mapping(source = "userConfig.mastery", target = "mastery")
    @Mapping(source = "userConfig.penetration", target = "penetration")
    @Mapping(source = "userConfig.baseEnergyRegen", target = "baseEnergyRegen")
    // 音擎信息
    @Mapping(source = "userConfig.armamentId", target = "armamentId")
    @Mapping(source = "userConfig.armamentBaseAtk", target = "armamentBaseAtk")
    @Mapping(source = "userConfig.armamentExtraBonus", target = "armamentExtraBonus")
    @Mapping(source = "armament.armamentName", target = "armamentName")
    @Mapping(source = "armament.armamentType", target = "armamentType")
    @Mapping(source = "armament.rarity", target = "armamentRarity")
    // 配置属性
    @Mapping(source = "userConfig.setBonus", target = "setBonus")
    @Mapping(source = "userConfig.coreSkillBonus", target = "coreSkillBonus")
    @Mapping(source = "userConfig.driveMainAttr1", target = "driveMainAttr1")
    @Mapping(source = "userConfig.driveMainAttr2", target = "driveMainAttr2")
    @Mapping(source = "userConfig.driveMainAttr3", target = "driveMainAttr3")
    @Mapping(source = "userConfig.driveMainAttr4", target = "driveMainAttr4")
    @Mapping(source = "userConfig.driveMainAttr5", target = "driveMainAttr5")
    @Mapping(source = "userConfig.driveMainAttr6", target = "driveMainAttr6")
    @Mapping(source = "userConfig.driveRandomAttr1", target = "driveRandomAttr1")
    @Mapping(source = "userConfig.driveRandomAttr2", target = "driveRandomAttr2")
    @Mapping(source = "userConfig.driveRandomAttr3", target = "driveRandomAttr3")
    @Mapping(source = "userConfig.driveRandomAttr4", target = "driveRandomAttr4")
    @Mapping(source = "userConfig.driveRandomAttr5", target = "driveRandomAttr5")
    @Mapping(source = "userConfig.driveRandomAttr6", target = "driveRandomAttr6")
    @Mapping(source = "agent", target = "agent")
    @Mapping(source = "skills", target = "skills")
    UserConfigDetailDto toDetailDto(
            UserConfig userConfig,
            Agent agent,
            Armament armament,
            List<AgentSkills> skills
    );

    // 批量详情转换（使用Map参数）
    default List<UserConfigDetailDto> toDetailDtoList(
            List<UserConfig> userConfigList,
            Map<String, Agent> agentMap,
            Map<String, Armament> armamentMap,
            Map<String, List<AgentSkills>> skillsMap
    ) {
        return userConfigList.stream()
                .map(config -> {
                    String agentId = config.getAgentId();
                    String armamentId = config.getArmamentId();
                    Agent agent = agentMap.getOrDefault(agentId, null);
                    Armament armament = armamentMap.getOrDefault(armamentId, null);
                    List<AgentSkills> skills = skillsMap.getOrDefault(agentId, null);
                    return toDetailDto(config, agent, armament, skills);
                })
                .toList();
    }
}